'use strict';
const mongoose = require('mongoose');
const config = require('../config/config.unittest.js')({});
const fs = require('fs');
const readline = require('readline');
const moment = require('moment');
const _ = require('lodash');

let ParserBetter = require('../app/utils/rss/parser');
let parserBetter = new ParserBetter({timeout: 10000});

const UserModel = require('../app/model/User')({
  mongoose,
});
const TopicModel = require('../app/model/Topic')({
  mongoose,
});
const TopicCollectModel = require('../app/model/TopicCollect')({
  mongoose,
});

async function connectDB() {
  try {
    await mongoose.connect('mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha');
    console.log('Database connection successful');
    // 其他逻辑
  } catch (err) {
    console.error('Database connection error', err);
  }
}

main();

async function main() {
  await connectDB();
  await crud();
  process.exit(0);
}


async function crud() {
  const topics = await TopicModel.find({tab: 'tech'}).skip(80000).limit(20000);
  for (let i = 0; i < topics.length; i++) {
    const topic = topics[i];
    setTechBoard(topic);
    await topic.save();
    console.log(i, topics.length);
  }
}


function setTechBoard(topic) {
  const title = topic.title.toLowerCase();

  // 架构
  if (title.includes('架构') || title.includes('分布式')
    || title.includes('系统设计') || title.includes('微服务')
    || title.includes('可用性') || title.includes('伸缩性')
    || title.includes('负载均衡') || title.includes('容器化')
  ) {
    topic.boards.push('架构');
  }


  // 后端
  if (title.includes('Java') || title.includes('python')
    || title.includes('node.js') || title.includes('go')
    || title.includes('php') || title.includes('c#')
    || title.includes('.net') || title.includes('spring')
    || title.includes('django') || title.includes('服务器')
    || title.includes('微服务') || title.includes('后端框架')
  ) {
    topic.boards.push('后端');
  }

  // 前端
  if (title.includes('前端') || title.includes('javascript')
    || title.includes('react') || title.includes('vue')
    || title.includes('angular') || title.includes('flutter')
    || title.includes('less') || title.includes('sass')
    || title.includes('css') || title.includes('ecmascript')
    || title.includes('webpack') || title.includes('vite')
    || title.includes('样式') || title.includes('localStorage')
  ) {
    topic.boards.push('前端');
  }


  // 测试
  if (title.includes('测') || title.includes('缺陷')) {
    topic.boards.push('测试');
  }

  // AI·大数据
  if (title.includes('人工智能') || title.includes('ai')
    || title.includes('机器学习') || title.includes('深度学习')
    || title.includes('大数据') || title.includes('数据挖掘')
    || title.includes('神经网络') || title.includes('tensorflow')
    || title.includes('pytorch') || title.includes('强化学习')
    || title.includes('数据仓库') || title.includes('etl')
  ) {
    topic.boards.push('AI·大数据');
  }

  // 数据库
  if (title.includes('数据库') || title.includes('mysql')
    || title.includes('oracle') || title.includes('mongo')
    || title.includes('tidb') || title.includes('sql')
    || title.includes('hbase') || title.includes('关系数据库')
    || title.includes('数据建模') || title.includes('事务')
    || title.includes('索引') || title.includes('事务')
  ) {
    topic.boards.push('数据库');
  }


  // 技术周刊
  if (title.includes('周刊') || title.includes('周报')
    || title.includes('掘金一周')) {
    topic.boards.push('技术周刊');
  }


  if (!topic.boards.length) {
    topic.boards.push('其他');
  }

}

